package com.ybkj.daijia.server.event.listener.errand;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.errand.OrderUp;
import com.ybkj.daijia.server.event.model.errand.ErrandOrderUpCancelEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ErrandOrderUpCancelEventListener implements
    ApplicationListener<ErrandOrderUpCancelEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ErrandOrderUpCancelEventListener.class);


    @Autowired
    private DriverService driverService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private ErrandService errandService;

    @Autowired
    private SmsService smsService;

    @Override
    @Async
    public void onApplicationEvent(ErrandOrderUpCancelEvent event) {
        OrderUp order = (OrderUp) event.getSource();
        Driver driver = event.getDriver();
        String who = event.getWho();

        boolean pushDriver = false;
        boolean pushPassenger = false;
        if ("driverCancel".equals(who)) {
            pushPassenger = true;
        } else if ("customerCancel".equals(who)) {
            pushDriver = true;
        } else if ("adminCancel".equals(who)) {
            pushDriver = true;
            pushPassenger = true;
        }

        logger.debug("order errandCancel event:{}", order.getOrderNo());

        if (pushDriver) {
            try {
                //发送设备推送
                PushDataV4 pushDataV4 = new PushDataV4();
                pushDataV4.setCode("0X05");
                pushDataV4.setData(order.getId());
                pushDataV4.setType("0X02");
                Map<String, String> extra = new HashMap<String, String>();
                extra.put("content", "您的工单被销单了,单号:" + order.getOrderNo());
                pushDataV4.setExtra(extra);

                boolean getuiOnline = false;
                if (StringUtils.isNotBlank(driver.getUserID())) {
                    getuiOnline = GeTuiEmployV4
                        .getClientOnline(driver.getUserID(), settingUtils.get());
                }

                if (getuiOnline) {
                    GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
                } else {
                    if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                        JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                    }
                }

            } catch (IOException e) {
                logger.debug("push error : " + e.getMessage());
            }
        }

        if (pushPassenger) {
            Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
            //给客户推送
            if (null != passenger) {
                passenger.setCid(memberService.findCid(passenger.getId()));
                if (StringUtils.isNotBlank(passenger.getCid())) {
                    try {
                        //发送设备推送
                        PushData pushData = new PushData();
                        pushData.setType("CANCEL_ORDER_ERRAND");
                        pushData.setTitle("跑腿销单");
                        pushData.setDescription("您的跑腿订单已被销单,单号:" + order.getOrderNo());
                        pushData.setData(order.getId());

                        GeTuiMember.push(passenger, pushData, settingUtils.get());
                    } catch (IOException e) {
                        logger.debug("push error : " + e.getMessage());
                    }
                }
            }
        }
    }


}
